package com.itouchtv.yang.apiCore;

import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import org.testng.Assert;

import java.io.File;
import java.util.Map;

public class Request {
    public static ValidatableResponse uploadPost(Map headers, String api, String parameterName, String filePath,Object... objects){
        File uploadFile = new File(filePath);
        ValidatableResponse response;
        if (uploadFile.exists()){
          response = RestAssured.
                    given().log().all().
                    headers(headers).multiPart(parameterName,uploadFile).when().post(api,objects).
                    then().log().all();
          return response;
        }else {
            Assert.fail("不存在的文件："+filePath);
            return null;
        }
    }

    public static ValidatableResponse post(Map headers,Object body,String api,Object... objects){
        ValidatableResponse response = RestAssured.
                given().
                headers(headers).request().body(body).when().post(api,objects).
                then();
        return response;
    }

    public static ValidatableResponse get(Map headers,Object body,String api,Object...objects){
        ValidatableResponse response =RestAssured.given()
                .headers(headers).request().body(body).when().get(api,objects)
                .then();
        return response;
    }

    public static ValidatableResponse put(Map headers,Object body,String api,Object...objects){
        ValidatableResponse response =RestAssured.given().log().all()
                .headers(headers).request().body(body).when().put(api,objects)
                .then().log().all();
        return response;
    }

    public static ValidatableResponse delete(Map headers,Object body,String api,Object...objects){
        ValidatableResponse response =RestAssured.given().log().all()
                .headers(headers).request().body(body).when().delete(api,objects)
                .then().log().all();
        return response;
    }

    public static byte[] getBodyAsByte(ValidatableResponse validatableResponse){
        return validatableResponse.extract().body().asByteArray();
    }

    public static Object getBody(ValidatableResponse validatableResponse,String path,String... arguments){
        return validatableResponse.extract().body().path(path,arguments);
    }
}
